<?php

namespace app\admin\model\article;

use think\Model;


class Article extends Model
{

    

    

    // 表名
    protected $name = 'article';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'is_shopdata_text',
        'createtime_text',
        'image_number_text',
        'zhu_images_text'
    ];

    public function getCreatetimeTextAttr($value,$data)
    {
        return date('Y-m-d');
    }

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    public function getZhuImagesTextAttr($value,$data)
    {
        if($data['zhu_images']!='')
        {
            return explode(',',$data['zhu_images']);
        }
        return $data['zhu_images'];
    }

    public function getImageNumberTextAttr($value,$data)
    {
        return $data['zhu_images']!=''?count(explode(',',$data['zhu_images'])) : 0;
    }

    
    public function getIsShopdataList()
    {
        return ['1' => __('Is_shopdata 1'), '2' => __('Is_shopdata 2')];
    }


    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getIsShopdataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_shopdata']) ? $data['is_shopdata'] : '');
        $list = $this->getIsShopdataList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function fenlei()
    {
        return $this->belongsTo('app\admin\model\article\Fenlei', 'fenlei_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
